import { defineStore } from 'pinia';
import { getCategoryListResultModel } from '/@/api/product/model/categoryModel';
import {
  getCategory1ListApi,
  getCategory2ListApi,
  getCategory3ListApi,
} from '/@/api/product/category';

interface CategoryState {
  category1Id: number | undefined;
  category2Id: number | undefined;
  category3Id: number | undefined;
  category1List: getCategoryListResultModel;
  category2List: getCategoryListResultModel;
  category3List: getCategoryListResultModel;
}

export const useCategoryStore = defineStore({
  id: 'app-category',
  state: (): CategoryState => ({
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
  }),
  actions: {
    async getCategory1List() {
      const category1List = await getCategory1ListApi();
      this.category1List = category1List;
    },
    async getCategory2List(category1Id: number) {
      const category2List = await getCategory2ListApi(category1Id);
      this.category2List = category2List;
      this.category1Id = category1Id;
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.category3List = [];
    },
    async getCategory3List(category2Id: number) {
      const category3List = await getCategory3ListApi(category2Id);
      this.category3List = category3List;
      this.category2Id = category2Id;
      this.category3Id = undefined;
    },
    setCategory3Id(category3Id: number) {
      this.category3Id = category3Id;
      return category3Id;
    },
    resetCategory() {
      this.category1Id = undefined;
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.category1List = [];
      this.category2List = [];
      this.category3List = [];
    },
  },
});
